<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class admin_model extends model {
	public function __construct() {
		$this->db_config = pc_base::load_config('database');
		$this->db_setting = 'default';
		$this->table_name = 'admin';
		parent::__construct();
	}
	
	//管理部门ID
	public function manager_deptids($uid)
	{
		return pc_base::load_model('staff_dept_model')->deptids($uid);
	}
	
	
	final public function _myInfo()
	{
		static $info =null;
		if($info==null){
			//$admindb = pc_base::load_model('admin_model');
			//$admindb->query("SELECT a.`userid`, a.`username`, a.`roleid`, a.`lastloginip`, `lastlogintime`, `email`, `mobile`, `realname`, `docid`, `posid`, `deptid`, `staff`")
			$info = $this->get_one(array("userid"=>$_SESSION['userid']),'`userid`, `username`, `roleid`, `roleids`, `lastloginip`, `lastlogintime`, `email`, `mobile`, `realname`, `docid`, `posid`, `deptid`, `staff`');
		}
		return $info;
	}
	
	final public function _mydeptids()
	{
		//$admindb = pc_base::load_model('admin_model');
		//我的信息
		$info = $this->_myInfo();
		//管理的部门及部门经理
		$adminId = $_SESSION['userid'];
		$list = $this->query_info("SELECT deptid from v9_staff_dept WHERE uid='$adminId' union SELECT id FROM v9_dept WHERE mid='$adminId'");
		$ret = array();
		if($list)$ret = $this->array2map($list,'','deptid');
		if($info['deptid'])array_push($ret,$info['deptid']);
		$ret = array_unique($ret);
		return $ret;
	}
	
	final public function _mydeptids2str()
	{
		$ret = $this->_mydeptids();
		if(is_array($ret))return implode(',',$ret);
		return '';
	}
	
	
	public function chkpresids($uid=0)
	{
		if(!$uid)$uid = $_SESSION['userid'];
		$chk = $this->chk_priv($uid);
		/*
		array(1=>array(2,3),2=>array(4,5));
		*/
		//print_r($chk);
		//
		//$steps = $this->_fsids();
		//print_r($steps);
		//print_r();
		//exit();
		$ids = array();
		if(is_array($chk))foreach($chk as $k=>$v){
			if(is_array($v))foreach($v as $sv){
				if(in_array($sv,$ids))continue;
				$ids = array_merge($ids,$this->_presid($sv));
			}
		}
		$ids = array_unique($ids);
		//print_r($ids);
		//exit();
		return $ids;
	}
	private function _fsids()
	{
		static $data = null;
		if($data==null){
			//$this->qury
			$this->query("select * from phpcms_work_flowstep order by listorder asc");
			$data = $this->fetch_array();
			$data = $this->array2map($data,'id');
		}
		return $data;
	}
	public function _presid($sid)
	{
		$sids = $this->_fsids();
		$ret = array();
		$ret[] = $sid;
		if($sids[$sid]['pid']){			
			$ret = array_merge($ret,$this->_presid($sids[$sid]['pid']));
		}
		return $ret;
	}
	
	public function info($uid)
	{
		return $this->get_one(array('userid'=>$uid));
	}
	
	public function chk_priv($uid)
	{
		$one = $this->get_one(array('userid'=>$uid));
		$ret = array();
		if($one['chkpriv']){
			$ret = json_decode($one['chkpriv'],true);
		}
		return $ret;
	}
	public function chk_privids($uid)
	{
		$ret = $this->chk_priv($uid);
		$a = array();
		if($ret && is_array($ret))foreach($ret as $v){
			$a = array_merge($a,array_values($v)); 
		}
		return $a;
	}
	
	public function infos($where='')
	{
		$list = $this->select($where,'`userid`, `username`, `roleid`, `lastlogintime`, `email`, `mobile`, `realname`, `docid`, `posid`, `deptid`, `staff`');
		return $this->array2map($list,'userid');
	}
	/**
	 * 管理员名称
	 * @param unknown_type $where
	 * @return Ambigous <multitype:, multitype:unknown >
	 */
	public function rnames($where='')//staff=1
	{
		$list = $this->select($where,'userid,realname');
		return $this->array2map($list,'userid','realname');
	}
	//
	public function posttree($pid=3)
	{
		static $lv=0;
		$lv++;
		$this->query("select id,parentid from phpcms_position where parentid='".$pid."'");
		$list = $this->fetch_array();
		//print_r($list);
		if(empty($list)){
			return false;
		}
		$data = array();
		foreach($list as $v){
			$ret = $this->posttree($v['id']);
			print_r($ret);
			$data[$v['id']] = $lv;
			if($ret){
				print_r($data);
				$data = array_merge($data,$ret);
			}
		}
		return $data;
	}
	
	public function admlist($where = '', $order = '', $page = 1, $pagesize = 20)
	{
		$where = to_sqls($where);
		$this->number = $this->count($where);
		$page = max(intval($page), 1);
		$offset = $pagesize*($page-1);
		$this->pages = pages($this->number, $page, $pagesize, '', array(), 10);
	
		if ($this->number > 0) {
			$swhere = $where?' where '.$where:'';
			$sorder  = $order?' order by '.$order:'';
			$this->query("SELECT * FROM phpcms_admin $swhere $sorder limit $offset,$pagesize");
			return $this->fetch_array();
		} else {
			return array();
		}
	}
}
?>